<template> 
    <div class="app-container">
        <el-card class="filter-container" shadow="never">
            <div>
                <i class="el-icon-search"></i>
                <span>筛选搜索</span>
                <el-button
                        style="float: right"
                        @click="handleSearchList()"
                        type="primary"
                        size="small">
                    查询结果
                </el-button>
                <el-button
                        style="float: right;margin-right: 15px"
                        @click="handleResetSearch()"
                        size="small">
                    重置
                </el-button>
            </div>
            <div style="margin-top: 15px">
                <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
                    <el-form-item label="输入搜索：">
                        <el-input style="width: 203px" v-model="listQuery.keyword" placeholder="商品名称"></el-input>
                    </el-form-item>
                    <el-form-item label="商品货号：">
                        <el-input style="width: 203px" v-model="listQuery.productSn" placeholder="商品货号"></el-input>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>
        <el-card class="operate-container" shadow="never">
            <i class="el-icon-tickets"></i>
            <span>数据列表</span>
            <el-button
                    class="btn-add"
                    @click="handleAddProduct()"
                    size="mini">
                添加
            </el-button>
        </el-card>
        <div class="table-container">
            <el-table ref="productTable"
                      :data="listTotal"
                      style="width: 100%;font-size: 14px"
                      @selection-change="handleSelectionChange"
                      v-loading="listLoading"
                      border
                      stripe>
                <el-table-column type="selection" width="60" align="center"></el-table-column>
                <el-table-column label="编号" width="140" align="center">
                    <template slot-scope="scope">{{scope.row.id}}</template>
                </el-table-column>
                <el-table-column label="商品图片" width="120" align="center">
                    <template slot-scope="scope"><img style="width: 100px;height: 100px" :src="scope.row.images[0]">
                    </template>
                </el-table-column>
                <el-table-column label="商品名称" align="center">
                    <template slot-scope="scope">
                        <p>{{scope.row.title}}</p>
                    </template>
                </el-table-column>
                <el-table-column label="价格"  align="center">
                    <template slot-scope="scope">
                        <p>价格：￥{{scope.row.price}}</p>
                    </template>
                </el-table-column>
                <el-table-column label="库存" align="center">
                    <template slot-scope="scope">{{scope.row.num}}</template>
                </el-table-column>
                <el-table-column label="所属分类"  align="center">
                    <template slot-scope="scope">{{scope.row.cid}}</template>
                </el-table-column>
                <el-table-column label="商品状态"  align="center">
                    <template slot-scope="scope">
                        <p>{{scope.row.status | verifyStatusFilter}}</p>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="280" align="center">
                    <template slot-scope="scope">
                        <p>
                            <el-button
                                    v-if="scope.row.status === 0"
                                    size="mini"
                                    type="success"
                                    @click="handleitemStart(scope.$index, scope.row)">上架
                            </el-button>
                            <el-button
                                    v-else
                                    size="mini"
                                    type="warning"
                                    @click="handleitemStop(scope.$index, scope.row)">下架
                            </el-button>
                            <el-button
                                    size="mini"
                                    type="primary"
                                    @click="handleUpdateProduct(scope.$index, scope.row)">编辑
                            </el-button>
                            <el-button
                                    size="mini"
                                    type="danger"
                                    @click="handleDelete(scope.$index, scope.row)">删除
                            </el-button>
                        </p>
                    </template>
                </el-table-column>
            </el-table>
        </div>
    </div>
</template>
<script>
    import {getItem560, getItem1184, delItem, itemStart, itemStop} from '/src/api/item'
    const defaultListQuery = {
        keyword: null,
        productSn: null,
    };
    export default {
        name: "productList",
        data() {
            return {
                editSkuInfo: {
                    dialogVisible: false,
                    productId: null,
                    productSn: '',
                    productAttributeCategoryId: null,
                    stockList: [],
                    productAttr: [],
                    keyword: null
                },
                operates: [
                    {
                        label: "商品上架",
                        value: "publishOn"
                    },
                    {
                        label: "商品下架",
                        value: "publishOff"
                    },
                    {
                        label: "设为推荐",
                        value: "recommendOn"
                    },
                    {
                        label: "取消推荐",
                        value: "recommendOff"
                    },
                    {
                        label: "设为新品",
                        value: "newOn"
                    },
                    {
                        label: "取消新品",
                        value: "newOff"
                    },
                    {
                        label: "转移到分类",
                        value: "transferCategory"
                    },
                    {
                        label: "移入回收站",
                        value: "recycle"
                    }
                ],
                operateType: null,


                list560: [],
                list1184: [],
                listTotal: [],
                listLoading: true,

                listQuery: Object.assign({}, defaultListQuery),
                selectProductCateValue: null,
                multipleSelection: [],
                productCateOptions: [],
                brandOptions: [],
                publishStatusOptions: [{
                    value: 1,
                    label: '上架'
                }, {
                    value: 0,
                    label: '下架'
                }],
                verifyStatusOptions: [{
                    value: 1,
                    label: '审核通过'
                }, {
                    value: 0,
                    label: '未审核'
                }]
            }
        },
        created() {
            this.getList();
            this.total = this.listTotal.length;
            this.getBrandList();
            this.getProductCateList();
        },
        watch: {

        },
        filters: {
            verifyStatusFilter(value) {
                if (value == 1) {
                    return '正常';
                } else if (value == 0) {
                    return '下架';
                }
            }
        },
        methods: {
            getProductSkuSp(row, index) {
                let spData = JSON.parse(row.spData);
                if (spData != null && index < spData.length) {
                    return spData[index].value;
                } else {
                    return null;
                }
            },
            getListTotal(arr) {
                for (let i = 0; i < arr.length; i++) {
                    const item = arr[i];
                    this.listTotal.push(item);
                }
            },
            //获取商品列表
            getList() {
                this.listLoading = true;
                getItem560().then(response => {
                    this.listLoading = false;
                    this.list560 = response.data.data;
                    this.getListTotal(response.data.data);
                });
                getItem1184().then(response => {
                    this.list1184 = response.data.data;
                    this.getListTotal(response.data.data);
                })
            },
            getBrandList() {
            },
            getProductCateList() {

            },
            //查询按钮
            handleSearchList() {
                // this.getList();
            },
            //添加商品
            handleAddProduct() {
                this.$router.push({path: '/product/add'});
            },
            handleSizeChange(val) {
                this.getList();
            },
            handleCurrentChange(val) {
                this.getList();
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleResetSearch() {
                this.selectProductCateValue = [];
            },
            //上架商品
            handleitemStart(index, row) {
                itemStart(row.id).then(res => {
                    if (res.data.result != null) {
                        this.listLoading = true;
                        this.list560 = [];
                        this.list1184 = [];
                        this.listTotal = [];
                        this.getList();
                        this.total = this.listTotal.length;
                        this.$message.success('操作成功')
                        this.listLoading = false;
                    } else {
                        this.$message.error('操作失败');
                    }
                })
            },
            //下架商品
            handleitemStop(index, row) {
                itemStop(row.id).then(res => {
                    if (res.data.result != null) {
                        this.listLoading = true;
                        this.list560 = [];
                        this.list1184 = [];
                        this.listTotal = [];
                        this.getList();
                        this.total = this.listTotal.length;
                        this.$message.success('操作成功')
                        this.listLoading = false;
                    } else {
                        this.$message.error('操作失败');
                    }
                })
            },
            //删除商品
            handleDelete(index, row) {
                this.$confirm('是否要进行删除操作?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    delItem(row.id).then(res => {
                        if (res.data.result === null) {
                            this.$message.success('删除成功');
                            this.list560 = [];
                            this.list1184 = [];
                            this.listTotal = [];
                            this.getList();
                            this.total = this.listTotal.length;
                        } else {
                            this.$message.error('删除失败');
                            this.$router.push('/404')
                        }
                    })
                });
            },
            //编辑
            handleUpdateProduct(index, row) {
                this.$router.push({path: '/product/update', query: {id: row.id}});
            },

        }
    }
</script>
<style></style>


